#!/bin/sh
# set up in your crontab
# * * * * * /alidata1/webapp/php/sportchat/bin/monitor_task.sh

# set the enviroment of the script
export FUEL_ENV=production
# define the task list .....
CRON_CHECKS=("weixincron:refreshToken demo_project" "weixincron:refreshJsticket demo_project")

# the webapp dir
BIN_DIR="/Users/Chris/Dev/php/wolfteam-fuelphp/web"
# the log dir
LOG_DIR="/Users/Chris/Dev/php/wolfteam-fuelphp/web"

PHP_CMD="/usr/local/php/bin/php"
PHP_INI="/usr/local/php/lib/php.ini"

CheckProcess()
{
  # 检查输入的参数是否有效
  if [ "$1" = "" ];
  then
    return 1
  fi

  #$PROCESS_NUM获取指定进程名的数目，为1返回0，表示正常，不为1返回1，表示有错误，需要重新启动
  PROCESS_NUM=`ps -ef | grep "$1" | grep -v "grep" | wc -l`
  if [ $PROCESS_NUM -eq 1 ];
  then
    return 1
  else
    return 0
  fi
}


cd $BIN_DIR
for ((i=0;i<${#CRON_CHECKS[@]};i++))
do
   #echo ${CRON_CHECKS[$i]}
   CheckProcess "oil r ${CRON_CHECKS[$i]}"
   r=$?
   #echo $r
   if [ $r -eq 0 ] ;then
    echo "Cannot found '${CRON_CHECKS[$i]}', try to run..."
    echo "exec '${PHP_CMD} -c ${PHP_INI} oil r ${CRON_CHECKS[$i]} >> ${LOG_DIR}/${CRON_CHECKS[$i]}  &'"

    eval "exec ${PHP_CMD} -c ${PHP_INI} oil r ${CRON_CHECKS[$i]}  &"
   fi
done
echo "check finish"

